<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/16 15:50
 * Desc:
 */
trait Trait_Api_Guide
{
    // 推荐
    public function recommend($params = array())
    {
        /**
         * @var $page
         * @var $pagesize
         */
        extract($params);

        $page     = !empty($page) ? $page : 1;
        $pagesize = !empty($pagesize) ? $pagesize : 10;
        $offset   = St_Page::offset($page, $pagesize);

        $list = DB::select(
            'a.id', 'a.work_time', 'a.guide_card_id', 'a.good_language',
            'a.service_city', 'b.litpic', 'b.truename', 'b.mobile', 'a.intro'
        )
            ->from(array('guide', 'a'))
            ->join(array('member', 'b'), 'inner')
            ->on('a.mid', '=', DB::expr('sline_b.mid'))
            ->where('a.status', '=', 1)
            ->and_where('b.mid', '>', 0)
            ->order_by('a.displayorder', 'asc')
            ->offset($offset)
            ->limit($pagesize)
            ->execute()
            ->as_array();

        foreach ($list as &$v) {
            $v['title']         = $v['truename'];
            $v['service_city']  = $v['service_city'] ?: '';
            $v['intro']         = $v['intro'] ?: '';
            $v['good_language'] = $v['good_language'] ?: '';
            $v['litpic']        = $this->img_url($v['litpic'], 94, 94);
            $v['icon_list']     = $this->icons($v['iconlist']);
        }

        return $list;
    }


    /**
     * 获取导游服务
     * @param $params
     * @return array
     * @throws Exception
     */
    public function get_guide_service($params)
    {
        /**
         * @var $page
         * @var $pagesize
         * @var $guide_id
         */
        extract($params);

        $guide = DB::select('b.truename','a.service_city')
            ->from(array('guide', 'a'))
            ->join(array('member', 'b'), 'inner')
            ->on('a.mid', '=', 'b.mid')
            ->where('a.id', '=', $guide_id)
            ->execute()
            ->current();
        if (empty($guide)) {
            throw new Exception('导游不存在');
        }

        $builder = DB::select()
            ->from('guide_service')
            ->where('status', '=', 6)
            ->and_where('guide_id', '=', $guide_id);

        $total = $builder->execute()->count();

        $offset = St_Page::offset($page, $pagesize);
        $list   = $builder->limit($pagesize)
            ->offset($offset)
            ->order_by('id', 'desc')
            ->execute()
            ->as_array();

        $out = array();
        foreach ($list as &$item) {
            $out[] = array(
                'id'           => $item['id'],
                'title'        => $item['title'],
                'price'        => $item['price'],
                'guide'        => $guide['truename'],
                'litpic'       => $this->img_url($item['litpic']),
                'service_city' => $guide['service_city'],
                'icon_list'    => $this->icons($item['iconlist']),
                'bookcount'    => Model_Member_Order_statistic::get_sell_num(self::TYPE_ID, $item['id']) + intval($item['bookcount']),
                'satisfyscore' => St_Functions::get_satisfy(self::TYPE_ID, $item['id'], $item['satisfyscore']),
                'series'       => '106'.$v['id']
            );
        }
        return array('total' => $total, 'list' => $out);
    }

    /**
     * 服务详情
     * @param $params
     * @return array|false|mixed
     * @throws Exception
     */
    public function get_service_info($params)
    {
        $id = $params['id'];

        $service = Model_Api_V2_Guide::get_info($id);
        if (!isset($service['id'])) {
            throw new Exception("未找到服务");
        }

        //导游详情
        $guide = DB::select('b.litpic', 'b.truename', 'b.mobile', 'a.service_city', 'a.mid', 'a.id')
            ->from(array('guide', 'a'))
            ->join(array('member', 'b'), 'inner')
            ->on('a.mid', '=', 'b.mid')
            ->where('a.id', '=', $service['guide_id'])
            ->execute()
            ->current();
        if (empty($guide)) {
            throw new Exception("未找到导游");
        }
        $paytype = $service['paytype'];
        // 不需要项
        unset($service['aid'],
            $service['webid'],
            $service['color'],
            $service['seotitle'],
            $service['keyword'],
            $service['description'],
            $service['tagword'],
            $service['ishidden'],
            $service['paytype'],
            $service['iconlist'],
            $service['themelist'],
            $service['supplierlist'],
            $service['templet'],
            $service['dingjin'],
            $service['jifenbook_id'],
            $service['jifentprice_id']
        );
        $uni_img = Model_Uniapp_Images::get_img_list(self::TYPE_ID,2);
        if(isset($uni_img['litpic'][$service['id']])){
            $service['litpic'] = $uni_img['litpic'][$service['id']];
            $service['piclist'] = $uni_img['piclist'][$service['id']];
        }
        $service['paytype']               = 1;
        $service['paytype_name']          = '全款支付';
        $service['piclist']               = $this->pic_list_url($service['piclist']);
        $service['guide']['title']        = $guide['truename'];
        $service['guide']['litpic']       = $this->img_url($guide['litpic'],750,750);
        $contacts_status                  = Model_Guide::check_contacts_show($guide['id'], $params['mid']);
        $service['guide']['mobile']       = $contacts_status == 2 ? $guide['mobile'] : Model_Guide::complex_mobile($guide['mobile']) . '(预约导游即可见号码)';
        $service['guide']['service_city'] = $guide['service_city'] ?: '';
        $service['enable_book']           = ($service['start_time'] > time() || $service['end_time'] < time()) ? 0 : 1;

        return $service;
    }

    /**
     * 价格日历
     * @param $service_id
     * @return array
     */
    public function calendar($service_id)
    {
        return Model_Guide_Suit_Price::calendar($service_id);
    }
}